from django.db import models
from django.db.models import UniqueConstraint

from Robots.models.RemoteOJ import RemoteOJ


class RobotAccount(models.Model):
    username = models.CharField(max_length=30, null=False, blank=False, verbose_name="用户名")
    password = models.CharField(max_length=30, null=False, blank=False, verbose_name="密码")
    remote_oj = models.ForeignKey(RemoteOJ, on_delete=models.PROTECT, default=1, related_name="oj_robots_account",
                                  verbose_name="所属OJ")

    is_active = models.BooleanField(default=True, verbose_name="是否可用")
    token = models.CharField(max_length=400, null=True, blank=True, verbose_name="token")

    comment = models.JSONField(max_length=200, null=True, blank=True, verbose_name="备注")

    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now_add=True, verbose_name='更新时间')
    is_delete = models.BooleanField(default=False, verbose_name='删除标记')

    class Meta:
        constraints = [
            UniqueConstraint(fields=['username', 'remote_oj'], name='unique_robot_account'),
        ]

        ordering = ["id"]

    def __str__(self):
        return self.remote_oj.name + " : " + self.username
